﻿using Abp.Dependency;
using Abp.Events.Bus.Handlers;
using RMM.Core.Entity.Massagistes;
using RMM.Core.Massagistes;
using RMM.Core.Orders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMM.WeChats.Events
{
    public class WeChatNotifyEventHandler : IEventHandler<WeChatNotifyEventData>, ITransientDependency
    {
        private readonly IOrderService _orderService;
        private readonly IMassagistService _massagistService;
        private readonly IMassagistExtendService _massagistExtendService;

        public WeChatNotifyEventHandler(IOrderService orderService, IMassagistService massagistService, IMassagistExtendService massagistExtendService)
        {
            _orderService = orderService;
            _massagistService = massagistService;
            _massagistExtendService = massagistExtendService;
        }

        public async void HandleEvent(WeChatNotifyEventData eventData)
        {
            var orderCode = eventData.OrderCode;
            var order = await _orderService.FirstOrDefaultAsync(p => p.OrderCode == orderCode);

            //修改订单状态
            await _orderService.ModifyAsync(order.Id, p => 
            {
                p.Status = 1;
                return Task.FromResult(p);
            });

            //更改技师状态为预约
            await _massagistService.ModifyAsync(order.MassagistId, (p) =>
            {
                p.Status = 1;
                return Task.FromResult(p);
            });
            //新增服务信息
            await _massagistExtendService.CreateAsync(new MassagistExtend()
            {
                MassagistId = order.MassagistId,
                Status = 0,
                StartTime = order.ServerStartDate.Value,
                EndTime = order.ServerEndDate.Value
            });
        }
    }
}
